package com.jintoufs.service.sys.impl;

import com.jintoufs.dao.QueryFilters;
import com.jintoufs.dao.sys.MessageDAO;
import com.jintoufs.dao.user.UserDAO;
import com.jintoufs.domain.sys.Message;
import com.jintoufs.domain.user.User;
import com.jintoufs.service.sys.MessageService;
import com.jintoufs.util.MailSendUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 */
@Service
public class MessageServiceImpl implements MessageService {
    @Resource
    private MessageDAO messageDAO;

    @Resource
    private UserDAO userDAO;

    @Resource
    private MailSendUtils mailSendUtils;

    @Override
    public List<Message> getByFilter(QueryFilters filters) {
        return messageDAO.getByFilter(filters);
    }

    @Override
    public Integer update(Message entity) {
        return messageDAO.update(entity);
    }

    @Override
    public Message insert(Message entity) {
        messageDAO.insert(entity);
        return entity;
    }

    @Override
    public void delete(Integer id) {
        messageDAO.delete(id);
    }

    @Override
    public List<Message> getByParams(Map<String, Object> params1) {
        return messageDAO.getByParams(params1);
    }

    @Override
    public void sendMessageForUsers(List<User> users,Map<String,String> params) {
        String msg = "新任务，请及时处理!";
        if(users.size() > 0){
            for(User user:users){
                Map<String,Object> userMap = userDAO.queryUserById(user.getId());
                //String mobilePhone = (String)userMap.get("mobilePhone");
                String email = (String)userMap.get("email");

                //发送短信
                //msgUtil.sendMsg(mobilePhone, msg);
                //邮件
                Map map = new HashMap();
                map.put("user",user);
                mailSendUtils.sendMail("【系统任务】新的审批任务",map,email,"shenhe.ftl");
                //系统消息提示
                Message message = new Message();
                message.setTitle("任务");
                message.setContent(msg);
                message.setUserId(user.getId());
                message.setCreateTime(new Date());
                message.setStatus(0);
                String href = "activitiManager/auditTask?proDefId="+params.get("proDefId")+"&taskId="+params.get("taskId")+
                        "&businessKey="+params.get("businessKey")+"&executionId="+params.get("executionId")+
                        "&type="+params.get("type")+"&proInsId="+params.get("proInsId")+"&status="+params.get("status");
                message.setHref(href);
                //messageDAO.insert(message);
            }
        }

    }
}
